app.controller('vipLogPageCtrl',function($scope,$timeout,$ionicSlideBoxDelegate,$http,$myconfig,$ionicConfig){

    //获取系统存储用户信息
    $scope.userMes = JSON.parse(localStorage.getItem('user'));
    // console.log($scope.userMes);
    //设置默认显示滑动框
    $scope.myActiveSlide = 0;
    // 设置默认设备名
    $scope.dev_name = '金鼻光';

    //定义饼形图渲染元素
    var myChart;

    //定义执行渲染请求优先级
    var is_up = 0;

    //切花饼形图时执行更新数据
    $scope.slideHasChanged = function(index){
        // console.log($scope.myActiveSlide);
        is_up = index;
        if(index == 0){
            myChart = echarts.init(document.getElementById('main11'));
            $scope.dev_name = '金鼻光';
        }else if(index == 1){
            $scope.dev_name = '焕颜宝';
            myChart = echarts.init(document.getElementById('main12'));
        }
        $scope.logList($scope.dev_name,$scope.userMes.id,1);
        $scope.bingData($scope.dev_name,$scope.userMes.id,myChart)
    };

    //设置页面加载方式
    $scope.vip_log_if = {
        hasmore_vip_log : false,
        page: 1,
        limit: 10
    };

    //预置显示数据空数组
    $scope.logListData = [];
    //请求接口获取列表内容
    $scope.logList = function(device,userid,ref){
        if(ref){
            $scope.vip_log_if.page = ref;
            $scope.logListData = [];
        }
        $http.get($myconfig.api.base_host + '/physicallogs/list_physicallogs?page=' + $scope.vip_log_if.page + '&limit=' + $scope.vip_log_if.limit + '&dev_name=' + device + '&user_id=' + userid)
        .then(function(res){
            // console.log(res.data.data);
            // 判断请求下的数据的条数
             if (res.data.data.length < 10) {
                    // 如果请求下的数据不足十条，则不进行下次请求
                    $scope.vip_log_if.hasmore_vip_log = true;
                    // 如果请求的数据大于0，则将数据拼接至原有数据之后
                    if (res.data.data.length > 0) {
                        //数据拼接
                        $scope.logListData = $scope.logListData.concat(res.data.data);
                    }
                        // console.log(res.data.data);
                    return;
                }
            // console.log(res.data.data);
            $scope.logListData = $scope.logListData.concat(res.data.data);
            $scope.vip_log_if.page += 1;
            $scope.$broadcast('scroll.infiniteScrollComplete');
        });
    };


    //请求饼形图数据
    $scope.bingData = function(device_name,use_id,MyChart){
        if(MyChart){
            myChart = MyChart;
        }else if(!MyChart){
            myChart = echarts.init(document.getElementById('main11'));
        }
        $http.get($myconfig.api.base_host + '/physicallogs/count_mode?dev_name=' + device_name + '&user_id=' + use_id)
        .then(function(res){
            // console.log(res.data.data);
            $scope.timeMin = 0;
            if(res.data.data.length){
                for(var i = 0;i<res.data.data.length;i++){
                    $scope.timeMin += parseInt(res.data.data[i].durations)/60;
                }
            }
            if(res.data.data.length == 0 || !res.data.success){
                option = {
                series : [
                        {
                            name: '设备使用统计',
                            type: 'pie',
                            radius: ['0%', '65%'],
                            center: ['50%', '50%'],
                            labelLine: {
                                normal: {
                                show: false}
                            },
                            data:[
                                {
                                    value:1,
                                    name:'暂无数据',
                                    itemStyle: {
                                        normal: {
                                            color: '#FEBD26',
                                            borderColor:'#FFF',
                                            borderWidth:5
                                        }
                                    }
                                },
                            ],

                            label: {
                                normal: {
                                    // formatter: '{d}%\n{b}',
                                    position: 'outside',
                                }
                            },
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
            }else if(res.data.data.length > 0){
                var data = [];
                var color = ['#43DA78','#FA5252','#FEBD26','#1A1AE6'];
                for(var i = 0; i<res.data.data.length; i++){
                    data.push({
                        value: res.data.data[i].durations,
                        name: res.data.data[i].mode,
                        itemStyle: {
                            normal: {
                                color: color[i],
                                borderColor: '#FFF',
                                borderWidth: 5
                            }
                        }
                    });
                }

                option = {
                series : [
                        {
                            name: '设备使用统计',
                            type: 'pie',
                            radius: ['0%', '65%'],
                            center: ['50%', '50%'],
                            labelLine: {
                                normal: {
                                show: false}
                            },
                            data: data,

                            label: {
                                normal: {
                                    formatter: '{d}%\n{b}',
                                    position: 'outside',
                                }
                            },
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
            }
            /*else if(res.data.data.length == 2){
                option = {
                series : [
                        {
                            name: '设备使用统计',
                            type: 'pie',
                            radius: ['0%', '65%'],
                            center: ['50%', '50%'],
                            labelLine: {
                                normal: {
                                show: false}
                            },
                            data:[
                                {
                                    value:res.data.data[0].durations,
                                    name:res.data.data[0].mode,
                                    itemStyle: {
                                        normal: {
                                            color: '#43DA78',
                                            borderColor:'#FFF',
                                            borderWidth:5
                                        }
                                    }
                                },
                                {
                                    value:res.data.data[1].durations,
                                    name:res.data.data[1].mode,
                                    itemStyle: {
                                        normal: {
                                            color: '#FA5252',
                                            borderColor:'#FFF',
                                            borderWidth:5

                                        }
                                    }
                                }
                            ],

                            label: {
                                normal: {
                                    formatter: '{d}%\n{b}',
                                    position: 'outside',
                                }
                            },
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
            }else if(res.data.data.length == 3){
                option = {
                     series : [
                        {
                            name: '设备使用统计',
                            type: 'pie',
                            radius: ['0%', '65%'],
                            center: ['50%', '50%'],
                            labelLine: {
                                normal: {
                                show: false}
                            },
                            data:[
                                {
                                    value:res.data.data[0].durations,
                                    name:res.data.data[0].mode,
                                    itemStyle: {
                                        normal: {
                                            color: '#43DA78',
                                            borderColor:'#FFF',
                                            borderWidth:5
                                        }
                                    }
                                },
                                {
                                    value:res.data.data[1].durations,
                                    name:res.data.data[1].mode,
                                    itemStyle: {
                                        normal: {
                                            color: '#FA5252',
                                            borderColor:'#FFF',
                                            borderWidth:5

                                        }
                                    }
                                },
                                {
                                    value:res.data.data[2].durations,
                                    name:res.data.data[2].mode,
                                    itemStyle: {
                                        normal: {
                                            color: '#FEBD26',
                                            borderColor:'#FFF',
                                            borderWidth:5
                                        }
                                    }
                                }
                            ],

                            label: {
                                normal: {
                                    formatter: '{d}%\n{b}',
                                    position: 'outside',
                                }
                            },
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
            }else if(res.data.data.length == 4){
                option = {
                    series : [
                            {
                                name: '设备使用统计',
                                type: 'pie',
                                radius: ['0%', '65%'],
                                center: ['50%', '50%'],
                                labelLine: {
                                    normal: {
                                    show: false}
                                },
                                data:[
                                    {
                                        value:res.data.data[0].durations,
                                        name:res.data.data[0].mode,
                                        itemStyle: {
                                            normal: {
                                                color: '#43DA78',
                                                borderColor:'#FFF',
                                                borderWidth:5
                                            }
                                        }
                                    },
                                    {
                                        value:res.data.data[1].durations,
                                        name:res.data.data[1].mode,
                                        itemStyle: {
                                            normal: {
                                                color: '#FA5252',
                                                borderColor:'#FFF',
                                                borderWidth:5

                                            }
                                        }
                                    },
                                    {
                                        value:res.data.data[2].durations,
                                        name:res.data.data[2].mode,
                                        itemStyle: {
                                            normal: {
                                                color: '#FEBD26',
                                                borderColor:'#FFF',
                                                borderWidth:5
                                            }
                                        }
                                    },
                                    {
                                        value:res.data.data[3].durations,
                                        name:res.data.data[3].mode,
                                        itemStyle: {
                                            normal: {
                                                color: '#1A1AE6',
                                                borderColor:'#FFF',
                                                borderWidth:5
                                            }
                                        }
                                    },
                                    // {value:135, name:'视频广告'}
                                ],

                                label: {
                                    normal: {
                                        formatter: '{d}%\n{b}',
                                        position: 'outside',
                                    }
                                },
                                itemStyle: {
                                    emphasis: {
                                        shadowBlur: 10,
                                        shadowOffsetX: 0,
                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                                    }
                                }
                            }
                        ]
                    };
            }*/
        myChart.setOption(option);

        });
    };


    //页面信息加载完毕后  执行渲染画面数据部分
    $scope.$on('$ionicView.afterEnter', function() {
        // var myChart = echarts.init(document.getElementById('main11'));
        // var myChart1 = echarts.init(document.getElementById('main12'));
        // myChart.setOption(option);
        // myChart1.setOption(option1);
        $ionicConfig.views.swipeBackEnabled(false);
        $scope.logList($scope.dev_name,$scope.userMes.id);
        $scope.bingData($scope.dev_name,$scope.userMes.id);
    }, false);
});
